package com.zycfc.zsf.validate;

import com.zycfc.zsf.boot.util.text.*;
import com.zycfc.zsf.boot.util.collection.*;
import java.util.*;
import com.alibaba.fastjson.*;
import com.zycfc.zsf.boot.core.common.*;
import org.slf4j.*;

public class ValidateHandler
{
    private static final Logger log;
    private static final String SLASH_SYMBOL = "/";
    private static final String COMBINE_SEPARATOR = "\\&";
    private static final String EQUAL_SEPARATOR = "\\=";
    private static final String COLON_SEPARATOR = ":";
    private static final String SEMICOLON_SYMBOL = ";";
    private static final String WELL_SYMBOL = "#";
    private static final String REQUEST_VALIDATE_RULE_KEY = "request.validate.rule";
    
    public static String handle(final String key, final String content) {
        if (StringUtils.isEmpty(key) || StringUtils.isEmpty(content)) {
            return content;
        }
        String result = new String(content);
        try {
            final List<String> ruleList = getRuleContent(key);
            if (CollectionUtils.isEmpty((Collection)ruleList)) {
                return content;
            }
            for (final String rule : ruleList) {
                ValidateHandler.log.info("ValidateHandler-\u63a5\u53e3:{}\u51c6\u5907\u8fdb\u884c\u89c4\u5219\u6821\u9a8c!", (Object)key);
                result = dealRule(key, rule, result);
            }
            return result;
        }
        catch (Throwable e) {
            ValidateHandler.log.error("ValidateHandler-\u63a5\u53e3:{}\u6821\u9a8c\u53d1\u751fException:", (Object)key, (Object)e);
            throw e;
        }
    }
    
    private static List<String> getRuleContent(final String key) {
        if (key.length() < 7 || key.indexOf("/", 5) < 0) {
            return null;
        }
        final String group = key.substring(5, key.indexOf("/", 5));
        final String rule = null;
        ValidateHandler.log.info("ValidateHandler-\u63a5\u53e3:{}\u83b7\u53d6\u5230\u7684\u89c4\u5219\u94fe\u4e3a:{}", (Object)key, (Object)rule);
        if (StringUtils.isEmpty(rule)) {
            return null;
        }
        final List<String> ruleList = new ArrayList<String>();
        final String[] ruleArr = rule.split(";");
        for (int i = 0; i < ruleArr.length; ++i) {
            if (!StringUtils.isEmpty(ruleArr[i])) {
                final String[] ruleContentArr = ruleArr[i].split("\\=");
                if (key.equals(ruleContentArr[0])) {
                    ruleList.add(ruleContentArr[1]);
                }
            }
        }
        ValidateHandler.log.info("ValidateHandler-\u63a5\u53e3:{}\u89c4\u5219\u96c6\u5408\u4e3a:{}", (Object)key, (Object)ruleList.toString());
        return ruleList;
    }
    
    private static String dealRule(final String key, final String rule, final String content) {
        final List<String> ruleInfoList = getRuleInfoList(rule);
        String result = content;
        for (final String ruleInfo : ruleInfoList) {
            result = doValidate(key, ruleInfo, result);
        }
        return result;
    }
    
    private static List<String> getRuleInfoList(final String rule) {
        final String realRule = rule.substring(1, rule.length() - 1);
        return Arrays.asList(realRule.split("#"));
    }
    
    private static String doValidate(final String key, final String ruleInfo, final String content) {
        try {
            final String[] ruleDtlArr = ruleInfo.split("\\&");
            final JSONObject jsonObject = JSONObject.parseObject(content);
            for (int i = 1; i < ruleDtlArr.length; ++i) {
                final String[] dtlArr = ruleDtlArr[i].split(":");
                final String reqValue = jsonObject.getString(dtlArr[0]);
                if (!StringUtils.equals((CharSequence)dtlArr[1], (CharSequence)reqValue)) {
                    return content;
                }
            }
            ValidateHandler.log.info("ValidateHandler-\u63a5\u53e3:{}\u547d\u4e2d\u89c4\u5219:{}", (Object)key, (Object)ruleInfo);
            final Validator validator = (Validator)SpringContextHelper.getBean(ruleDtlArr[0].split(":")[1]);
            return validator.validate(content);
        }
        catch (Exception e) {
            ValidateHandler.log.error("ValidateHandler-\u63a5\u53e3:{}\u6267\u884c\u89c4\u5219:{}\u53d1\u751fException:", new Object[] { key, ruleInfo, e });
            throw e;
        }
    }
    
    static {
        log = LoggerFactory.getLogger((Class)ValidateHandler.class);
    }
}
